import {getRouters} from "@/api/menu";
import {constantRoutes} from "@/router";
import Layout from '@/layout/index.vue';

const permissions = {
    state: {
        sidebarRouters: []
    },
    mutations: {
        SET_SIDEBAR_ROUTES: (state, routes) => {
            state.sidebarRouters = routes;
        }
    },
    actions: {
        GenerateRoutes({commit}) {
            return new Promise(resolve => {
                getRouters().then(res => {
                    const rdata = JSON.parse(JSON.stringify(res.data));
                    const sdata = JSON.parse(JSON.stringify(res.data));
                    const sidebarRoutes = filterAsyncRouter(sdata);
                    const rewriteRoutes = filterAsyncRouter(rdata);
                    commit('SET_SIDEBAR_ROUTES', constantRoutes.concat(sidebarRoutes));
                    resolve(rewriteRoutes)
                })
            })
        }
    }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
    return asyncRouterMap.filter(route => {
        if (route.component) {
            if (route.component === 'Layout') {
                route.component = Layout;
            } else {
                route.component = loadView(route.component);
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children);
        }
        return true;
    })
}

function loadView(view) {
    return (resolve) => require([`@/views/admin/${view}`], resolve)
    // 使用 import 实现生产环境的路由懒加载
    //return () => import(`@/views/admin/${view}`)
}

export default permissions
